let yearselect = document.querySelector("#year-select")
let monthselect = document.querySelector("#month-select")
let dayselect = document.querySelector("#day-select")
let hourselect = document.querySelector("#hour-select")
let miniteselect = document.querySelector("#minite-select")
let secondselect = document.querySelector("#second-select")
let result = document.querySelector("#result-wrapper")

function createOption(ele, start, end) {
    for (let i = start; i <= end; i++) {
        let op = document.createElement("option")
        if (ele == hourselect || ele == miniteselect || ele == secondselect) {
            i = i < 10 ? "0" + i : i
        }
        op.value = i
        op.innerText = i
        ele.appendChild(op)
    }
}

function getWeek(date) {
    let weeklist = {
        0: "星期日",
        1: "星期一",
        2: "星期二",
        3: "星期三",
        4: "星期四",
        5: "星期五",
        6: "星期六"
    }
    return weeklist[date.getDay()]
}

function formatDate() {
    let year = yearselect.value,
        month = monthselect.value,
        day = dayselect.value,
        hour = hourselect.value,
        minite = miniteselect.value,
        second = secondselect.value,
        d,
        h,
        m,
        s
    let nowdate = new Date()
    nowdate = nowdate.getTime()
    let selecttime = new Date(year, month - 1, day, hour, minite, second)
    selectdate = selecttime.getTime()
    let distance = nowdate - selectdate
    if (distance < 0) {
        d = Math.floor(-distance / 1000 / 3600 / 24)
        h = Math.floor(-distance % (1000 * 3600 * 24) / 1000 / 3600)
        m = Math.floor(-distance % (1000 * 3600 * 24) % (1000 * 3600) / 1000 / 60)
        s = Math.floor(-distance % (1000 * 3600 * 24) % (1000 * 3600) % (1000 * 60) / 1000)
        result.innerHTML = "现在距离  " + year + " 年 " + month + " 月 " + day + " 日 " + getWeek(selecttime) + "&nbsp;" + hour + " : " + minite + " : " + second + "  还有 " + d + " 天 " + h + " 小时 " + m + " 分 " + s + " 秒"
    } else {
        d = Math.floor(distance / 1000 / 3600 / 24)
        h = Math.floor(distance % (1000 * 3600 * 24) / 1000 / 3600)
        m = Math.floor(distance % (1000 * 3600 * 24) % (1000 * 3600) / 1000 / 60)
        s = Math.floor(distance % (1000 * 3600 * 24) % (1000 * 3600) % (1000 * 60) / 1000)
        result.innerHTML = "现在距离  " + year + " 年 " + month + " 月 " + day + " 日 " + getWeek(selecttime) + "&nbsp;" + hour + " : " + minite + " : " + second + "  已经过去 " + d + " 天 " + h + " 小时 " + m + " 分 " + s + " 秒"
    }

    return nowdate
}
function removedayselect() {
    for (let i = dayselect.length - 1; i >= 0; i--) {
        // dayselect.remove(dayselect.length - 1)
        dayselect[i].parentNode.removeChild(dayselect[i])
    }
}

function judgeday() {
    let year = yearselect.value
    let month = monthselect.value
    let day = 30
    let isleapyear = false
    removedayselect()
    // dayselect.innerHTML = ""
    if ((year % 400 == 0 && year % 3200 != 0) || (year % 4 == 0 && year % 100 != 0) || (year % 172800 == 0)) {
        isleapyear = true
    }
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        createOption(dayselect, 1, 31)
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        createOption(dayselect, 1, 30)
    } else if (month == 2) {
        if (isleapyear) {
            createOption(dayselect, 1, 29)
        } else {
            createOption(dayselect, 1, 28)
        }
    }
}

for (let i = 0, len = document.querySelectorAll("select"); i < len.length; i++) {
    len[i].addEventListener("change", formatDate)
}

createOption(yearselect, 2000, 2032)
createOption(monthselect, 1, 12)
createOption(dayselect, 1, 31)
createOption(hourselect, 0, 23)
createOption(miniteselect, 0, 59)
createOption(secondselect, 0, 59)

yearselect.addEventListener("change", judgeday)
monthselect.addEventListener("change", judgeday)

formatDate()
setInterval(formatDate,1000)